package avicit.bdp.dgs.standard.util;

import avicit.bdp.core.constant.Constants;
import avicit.bdp.core.constant.ProcessConstants;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dds.dispatch.enums.TaskType;
import avicit.bdp.dds.dispatch.model.ProcessData;
import avicit.bdp.dds.dispatch.model.TaskNode;
import avicit.bdp.dds.dispatch.process.Property;
import avicit.bdp.dds.dispatch.task.conditions.ConditionsParameters;
import avicit.bdp.dds.dispatch.task.standard.DataElementEvaluateTaskParameters;
import avicit.bdp.dgs.standard.dto.DataElementEvaluateTaskDTO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 数据标准评估任务构建工具类
 *
 * @author mayi
 */
public class DataElementEvaluateProcessDefinitionUtil {

  public static Map<String, Object> generateProcessDefinition(DataElementEvaluateTaskDTO taskDTO) {
    Map<String, Object> definitionMap = new HashMap<>(16);
    definitionMap.put(ProcessConstants.ID, taskDTO.getId());
    definitionMap.put(ProcessConstants.PROJECT_ID, 123456);
    definitionMap.put(ProcessConstants.TYPE, ProcessConstants.JOB_DATA_ELEMENT_EVALUATE);
    definitionMap.put(ProcessConstants.CONNECTS, "");
    definitionMap.put(ProcessConstants.LOCATIONS, "");
    definitionMap.put(ProcessConstants.NAME, taskDTO.getName());
    definitionMap.put(ProcessConstants.REMARK, taskDTO.getRemark());
    definitionMap.put(ProcessConstants.RELEASE_STATE, 0);
    definitionMap.put(
        ProcessConstants.PROCESS_DEFINITION_JSON,
        JSONUtils.toJsonString(generateProcessData(taskDTO)));
    definitionMap.put(ProcessConstants.EXTRA_PARAMS, "");
    return definitionMap;
  }

  private static ProcessData generateProcessData(DataElementEvaluateTaskDTO taskDTO) {
    ProcessData processData = new ProcessData();
    processData.setTasks(generateTaskNode(taskDTO));
    return processData;
  }

  private static List<TaskNode> generateTaskNode(DataElementEvaluateTaskDTO taskDTO) {

    List<TaskNode> taskNodeList = new LinkedList<>();
    TaskNode taskNode = new TaskNode();
    taskNode.setId(ProcessConstants.TASKS + Constants.SUBTRACT_STRING + 1);
    taskNode.setType(TaskType.DATA_ELEMENT_EVALUATE.name());
    taskNode.setName(taskDTO.getName());
    taskNode.setDesc(taskDTO.getRemark());
    taskNode.setConditionResult(JSONUtils.toJsonString(new ConditionsParameters()));

    DataElementEvaluateTaskParameters parameters = new DataElementEvaluateTaskParameters();
    parameters.setDataElementIds(taskDTO.getDataElementIds());
    parameters.setTaskId(taskDTO.getId());

    List<Property> localParams = new ArrayList<>();
    parameters.setLocalParams(localParams);

    ConditionsParameters conditionsParameters = new ConditionsParameters();
    taskNode.setConditionResult(JSONUtils.toJsonString(conditionsParameters));
    taskNode.setParams(JSONUtils.toJsonString(parameters));
    taskNodeList.add(taskNode);
    return taskNodeList;
  }
}
